<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>购物车</title>
    <%@include file="/pages/common/head_info.jsp" %>
    <link type="text/css" rel="stylesheet" href="static/css/index.css" >
    <link type="text/css" rel="stylesheet" href="static/css/head.css" >

</head>
<body>

<div class="head">
    <div class="logo">
        <h1 style="margin-top:0;"><a href="pages/client/index.jsp">线上社区超市</a></h1>
    </div>
    <div class="search">
        <input type="text" name="搜索" placeholder="搜索">
        <button>搜索</button>
    </div>
    <div>
        <c:if test="${empty sessionScope.userId}">
            <div class="userdata" style="width: 300px;margin-right: 250px;">
                <a href="pages/user/regist.jsp"><h3>注册</h3></a>
                <a href="pages/user/login.jsp"><h3>登录</h3></a>
            </div>
        </c:if>
        <c:if test="${not empty sessionScope.userId}">
            <div class="userdata" style="width: 400px;margin-right: 150px;">
                <a href="userServlet?action=logout"><h3 style="font-size: 10px;">注销</h3></a>
                <a href="orderServlet?action=showMyOrder"><h3 style="font-size: 10px;">我的订单</h3></a>
                <a href="pages/user/userdata.jsp"><h3 style="font-size: 10px;">个人信息</h3></a>
            </div>
        </c:if>
    </div>

</div>

<div class="main">

    <table style="margin-top:20px;">
        <tr>
            <td>商品名称</td>
            <td>数量</td>
            <td>单价</td>
            <td>金额</td>
            <td>操作</td>
        </tr>

        <c:if test="${empty requestScope.cartInfo}">
            <tr>
                <td colspan="5">
                    <a href="pages/client/index.jsp" style="text-decoration:none;width:100%;">当前购物车为空，快去添加商品！</a>
                </td>
            </tr>
        </c:if>

        <%--输出购物车信息--%>
        <c:forEach items="${requestScope.cartInfo.items}" var="item">
            <tr>
                <td>${item.name}</td>
                <td>
                    <a class="subCountClass" count="${item.count-1}" href="cartServlet?action=updateCount&itemId=${item.itemId}&count=${item.count-1}" style="text-decoration:none; color: darkgray;width:25px;height:25px;line-height:20px;color:black;">-</a>
                        <span style="font-size: 20px;margin: 10px;">${item.count}</span>
                    <a href="cartServlet?action=updateCount&itemId=${item.itemId}&count=${item.count+1}" style="text-decoration:none; color: black; width:25px;height:25px;line-height:20px;color:black;">+</a>
                </td>
                <td>${item.unitPrice}</td>
                <td>${item.totalPrice}</td>
                <td><a class="deleteClass" href="cartServlet?action=deletebook&itemId=${item.itemId}">删除</a></td>

                <c:forEach items="${requestScope.stockOut}" var="stockOut">
                    <c:if test="${stockOut == item.bookId}">
                        <td style="color: red">结算失败，商品库存不足</td>
                    </c:if>
                </c:forEach>
            </tr>
        </c:forEach>

    </table>


    <c:if test="${not empty requestScope.cartInfo}">
        <div class="cart_info">
            <span class="cart_span">购物车中共有<span class="b_count" style="color:red;">${requestScope.cartInfo.totalCount}</span>件商品</span>
            <span class="cart_span">总金额<span class="b_price" style="color:red;">${requestScope.cartInfo.totalPrice}</span>元</span>
            <div style="padding-top:20px;">
                <span class="cart_span"><a class="clear" href="cartServlet?action=clear" style="width:100px;">清空购物车</a></span>
                <span class="cart_span"><a class="createOrderClass" href="orderServlet?action=createOrder">去结账</a></span>
            </div>

        </div>
    </c:if>





</div>

</body>
</html>

<script type="text/javascript">

    $(function () {

        // 给删除item的a标签绑定单击事件，用于提示是否需要删除
        $("a.deleteClass").click(function () {
            // alert("aaa");
            return confirm("确定要删除 " + $(this).parent().parent().find("td:first").text())
        });

        // 给清空购物车的a标签绑定单击事件，用于提示是否需要删除
        $("a.clear").click(function () {
            // alert("aaa");
            return confirm("确定要清空购物车")
        });

        //减少数量时，减少后的数量不能少于1
        $("a.subCountClass").click(function () {

            let count = $(this).attr("count");
            if(count <= 0){
                return false;
            }
        });



        $("a.createOrderClass").click(function () {

            <%
                 Object cartInfo = request.getAttribute("cartInfo");
                 request.getSession().setAttribute("cartInfo",cartInfo);

            %>

        });

    })
</script>